package com.casic.modules.sys.controller;

import com.casic.common.utils.Constant;
import com.casic.common.utils.R;
import com.casic.common.validator.ValidatorUtils;
import com.casic.common.validator.group.AddGroup;
import com.casic.common.validator.group.UpdateGroup;
import com.casic.modules.sys.entity.SysDeptEntity;
import com.casic.modules.sys.entity.SysRoleEntity;
import com.casic.modules.sys.entity.SysUserEntity;
import com.casic.modules.sys.service.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 部门管理
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-06-20 15:23:47
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController {
	@Autowired
	private SysDeptService sysDeptService;

	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysRoleDeptService sysRoleDeptService;

	@Autowired
	private SysUserDeptService sysUserDeptService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public List<SysDeptEntity> list(){
		List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<String, Object>());
		return deptList;
	}

	/**
	 * 选择部门(添加、修改菜单)
	 */
	@RequestMapping("/select")
	public R select(){
		List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<String, Object>());

		//添加一级部门
		if(Constant.SUPER_ADMIN.equals(getUserId())){
			SysDeptEntity root = new SysDeptEntity();
			root.setDeptId("0");
			root.setName("一级部门");
			root.setParentId("-1");
			root.setOpen(true);
			deptList.add(root);
		}

		return R.ok().put("deptList", deptList);
	}

	/**
	 * 上级部门Id(管理员则为0)
	 */
	@RequestMapping("/info")
	public R info(){
		String deptId = "0";
		if(!Constant.SUPER_ADMIN.equals(getUserId())){
			SysDeptEntity dept = sysDeptService.queryObject(getDeptId()+"");
			deptId = dept.getParentId();
		}
		return R.ok().put("deptId", deptId);
	}
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{deptId}")
	public R info(@PathVariable("deptId") String deptId){
		SysDeptEntity dept = sysDeptService.queryObject(deptId);
		
		return R.ok().put("dept", dept);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody SysDeptEntity dept){
		ValidatorUtils.validateEntity(dept, AddGroup.class);
		sysDeptService.save(dept);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody @Validated(UpdateGroup.class) SysDeptEntity dept, Errors errors){

	    if(errors.hasErrors()) {
            return R.error(400, errors.getAllErrors().get(0).getDefaultMessage());
        }

	    if(sysDeptService.deptCodeValid(dept)) {
            return R.error("部门编号已存在");
        }

        sysDeptService.update(dept);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(String deptId){
		//判断是否有子部门
		List<String> deptList = sysDeptService.queryDetpIdList(deptId);
		if(deptList.size() > 0){
			return R.error("请先删除子部门");
		}
		List<SysUserEntity> userList=sysUserService.findByDeptId(deptId);
		if(userList.size()>0){
			return R.error("请先解除用户部门绑定该部门");
		}
		List<SysRoleEntity> roleList=sysRoleService.findByDeptId(deptId);
		if(roleList.size()>0){
			return R.error("请先解除角色绑定该部门");
		}
		List<String> roleDeptList=sysRoleDeptService.findByDeptId(deptId);
		if(roleDeptList.size()>0){
			return R.error("请先解除数据权限绑定该部门");
		}
		sysDeptService.delete(deptId);
		return R.ok();
	}


	@RequestMapping("/selectByUserId")
	public R selectByUserId(){

		SysUserEntity sysUserEntity= (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
		List<SysDeptEntity> deptList = sysDeptService.queryListByUserId(sysUserEntity.getUserId());
		return R.ok().put("deptList", deptList);
	}
	
}
